<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button  type="primary" @click="handleCreate">
          新增任务
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label:'编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
                label:'删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Modal @register="registerModal" @success="handleSuccess"/>
  </div>
</template>
<script lang="ts">
import {defineComponent, nextTick, ref} from 'vue';
import {BasicTable, TableAction, useTable} from '/@/components/Table';
import {TaskCenterDel, TaskCenterList} from '/@/api/taskCenter/taskCenter';
import {useModal} from '/@/components/Modal';
import Modal from './Modal.vue';
import {columns, searchFormSchema} from './schema.data';
import {usePermission} from '/@/hooks/web/usePermission';

export default defineComponent({
  name: 'TaskCenter',
  components: {BasicTable, Modal, TableAction},
  setup() {
    const {hasPermission} = usePermission();
    const [registerModal, {openModal}] = useModal();
    const [registerTable, {reload, expandAll,setProps,getDataSource}] = useTable({
      title: `任务列表`,
      api: TaskCenterList,
      columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      isTreeTable: true,
      pagination:{
        pageSize: 10,
        pageSizeOptions:['10', '20', '50', '100', '500']
      },
      striped: false,
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: false,
      canResize: false,
      actionColumn: {
        title: '操作',
        dataIndex: 'action',
        slots: {customRender: 'action'},
        fixed: 'right',
        ifShow: true,
      },
      afterFetch:(e)=>{
        setProps({
          title:`任务中心(${e.length})`
        })
      }
    });


    function handleCreate() {
      openModal(true, {
        isUpdate: false,
      });
    }

    function handleEdit(record: Recordable) {
      openModal(true, {
        record,
        isUpdate: true,
      });
    }

    function handleDelete(record: Recordable) {
      TaskCenterDel(record.id).then(() => {
        reload();
      });
    }

    function handleSuccess() {

      reload();
    }

    function onFetchSuccess() {
      nextTick(expandAll);
    }

    return {
      registerTable,
      registerModal,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSuccess,
      hasPermission,
      onFetchSuccess,
      setProps
    };
  },
});
</script>
